package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.app.App;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.data.PrefsUtils;
import com.yuyh.library.utils.toast.ToastUtils;

import butterknife.BindView;

/**
 * Created by wangguodong on 2017/9/6.
 */

public class MessageTipSetActivity extends BaseSwipeBackActivity implements CompoundButton.OnCheckedChangeListener{

    @BindView(R.id.is_notification)
    Switch isNotifaction;

    String needAuth;
    String newNotification;
    String searchMobile;
    String userId;

    PGService mPgService;

    public static void start(Activity activity ){
        Intent intent = new Intent(activity, MessageTipSetActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.msg_tip);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();

        //这里应该获取隐私设置，更新UI
        updateUI();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_message_tip_set;
    }

    /**
     * 更新 界面 信息
     */
    private void updateUI() {

        UserEntivity user = ToolsUtils.getUser();
        if (user != null) {
            userId = "" + user.getId();
            searchMobile = user.getSearchMobile();
            needAuth = user.getNeedAuth();
            newNotification = user.getNewNotification();

            isNotifaction.setChecked(judgeValue(newNotification));

            isNotifaction.setOnCheckedChangeListener(this);
        }
    }

    /**
     * 判断
     * 1 true      0 false
     */
    public boolean judgeValue(String value) {
        return "1".equals(value);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            newNotification = "1";
        } else {
            newNotification = "0";
        }

        if (isChecked){//接收通知
            new PrefsUtils(App.getInstance(), Constant.NEW_NOTIFICTION).put(Constant.NEW_NOTIFICTION,Constant.NEW_NOTIFICTION_A);
        }else {//不接收通知
            new PrefsUtils(App.getInstance(),Constant.NEW_NOTIFICTION).put(Constant.NEW_NOTIFICTION,Constant.AGREE_ADD_FRIEND_N);
        }
        updatePrivateSetting();
    }

    /**
     * 修改 隐私设置 网络请求
     */
    public void updatePrivateSetting() {

        if (!ToolsUtils.currentNetState(this)) {
            return;
        }

        mPgService.updatePrivateSetting(needAuth, newNotification, searchMobile, userId).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                Log.i("info","ex=="+ex.getDisplayMessage()+ex.getCode());
                new ToastUtils().showSingleToast(ex.getDisplayMessage());
            }

            @Override
            public void onNext(ValidateEntivity s) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        searchMobile = searchMobile + "";
                        UserEntivity user = ToolsUtils.getUser();
                        if (user != null) {
                            user.setSearchMobile(searchMobile);
                            user.setNeedAuth(needAuth);
                            user.setNewNotification(newNotification);
                            user.save();
                        }
                    }
                }.start();
            }
        });
    }

}
